import json
import os
from datetime import datetime

import requests

from remoteDB import ArticleDB


def listfile():
    db = ArticleDB()
    path = "/mnt/pdf/tasks/"
    files = os.listdir(path)
    for filename in files:
        db.insertIntoOssFile(filename)


if __name__ == '__main__':
    db = ArticleDB()
    index = 42035
    while True:
        id, docname = db.getOssFile(index)
        if docname:
            try:
                devDataDir = '/data/samereport/'
                date = datetime.now().strftime('%Y%m%d')
                if not os.path.exists(devDataDir + date):
                    os.mkdir(devDataDir + date)
                hours = datetime.now().strftime('%Y%m%d%H')
                path = devDataDir + date + '/' + hours + '/'
                if not os.path.exists(path):
                    os.mkdir(path)
                docDir = path + docname
                docname = docname.replace("#", "%23")
                res = requests.get('http://ixueshu.samereport.com/' + docname, stream=True)
                origindocname = docname.replace("%23", "#")
                with open(docDir, 'wb') as f:
                    f.write(res.content)
                params = {
                    'file': origindocname
                }
                headers = {'Content-Type': 'application/json'}
                jsonParams = json.dumps(params)
                res = requests.post('http://api.samereport.com/v1/admin/oss/remove', headers=headers, data=jsonParams)
                db.updateOssFileStatus(id, 1)
                index = id + 1
            except:
                index = id + 1



